package com.block.datapush.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.block.common.service.LogFactory;
import com.block.datapush.common.factory.FilterFactory;
import com.block.datapush.common.model.param.FilterHandlerParam;
import com.block.datapush.common.model.result.FilterHandlerResult;
import com.block.datapush.common.service.IDataFilterHandlerService;
import com.block.datapush.common.service.ISpecialDataFilterHandlerService;
import com.block.datapush.common.service.ISpreadDataFilterHandlerService;
import com.block.db.entity.*;
import com.block.db.mapper.SpecialCostomInfoMapper;
import com.block.db.mapper.SpecialCostomInfoProfitMapper;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;

@Service
public class DataFilterHandlerService implements IDataFilterHandlerService, ISpecialDataFilterHandlerService, ISpreadDataFilterHandlerService {

    @Resource
    SpecialCostomInfoMapper specialCostomInfoMapper;
    @Resource
    SpecialCostomInfoProfitMapper specialCostomInfoProfitMapper;

    private static Logger pushLog = LogFactory.getPushLog();

    /**
     * 执行条件过滤
     * @param itemInfo
     * @param memberExtInfo
     * @return
     */
    @Override
    public FilterHandlerResult execCondiFilter(SpecialCostomItemInfo itemInfo, MemberExtInfo memberExtInfo){

        SpecialCostomInfo costomInfo = specialCostomInfoMapper.selectById(itemInfo.getInfoId());

        String nowDate = LocalDate.now().toString();
        SpecialCostomInfoProfit specialCostomInfoProfit = specialCostomInfoProfitMapper.getEntityByDateInfoAndInfoId(String.valueOf(costomInfo.getId()), nowDate);

        FilterHandlerParam handlerParam = new FilterHandlerParam();

        handlerParam.setItemId(String.valueOf(itemInfo.getId()));
        handlerParam.setParentId(String.valueOf(costomInfo.getId()));

        handlerParam.setPushNumber(costomInfo.getPushNumber());
        if (ObjectUtil.isNotEmpty(specialCostomInfoProfit)){
            handlerParam.setSuccessNumber(specialCostomInfoProfit.getPushNumber());
        }else{
            handlerParam.setSuccessNumber(0);
        }

        handlerParam.setStatus(itemInfo.getStatus());
//        handlerParam.setRemainAmount(costomInfo.getRemainAmount());
//        handlerParam.setPriceAmount(itemInfo.getPriceAmount());
        handlerParam.setNotPushDayWeek(itemInfo.getNotPushDayWeek());
        handlerParam.setIdCard(itemInfo.getIdcardPrefix());

        handlerParam.setInChannelArray(itemInfo.getInChannelArray());
        handlerParam.setOutChannelArray(itemInfo.getOutChannelArray());

        handlerParam.setInCityArray(itemInfo.getCityName());
        handlerParam.setOutCityArray(itemInfo.getOutCityArray());

        handlerParam.setMinAge(itemInfo.getMinAge());
        handlerParam.setMaxAge(itemInfo.getMaxAge());
        handlerParam.setStartTime(itemInfo.getStarttime());
        handlerParam.setEndTime(itemInfo.getEndtime());

        handlerParam.setLoanAmount(itemInfo.getLoanAmount());
        handlerParam.setZhiMa(itemInfo.getZhima());
        handlerParam.setHuaBei(itemInfo.getHuabei());
        handlerParam.setBaiTiao(itemInfo.getBaitao());
        handlerParam.setXueLi(itemInfo.getXueli());

        handlerParam.setHouse(itemInfo.getHouse());
        handlerParam.setCar(itemInfo.getCar());
        handlerParam.setGjj(itemInfo.getGjj());
        handlerParam.setBaoDan(itemInfo.getInsurance());
        handlerParam.setSheBao(itemInfo.getSocialInsurance());
        handlerParam.setEnterpriseOwner(itemInfo.getEnterpriseOwner());
        handlerParam.setYuQi(itemInfo.getYuqi());

        handlerParam.setOrFilter(itemInfo.getOrFilter());

        FilterHandlerResult handlerResult = FilterFactory.execFilterChainService(handlerParam, memberExtInfo);
        pushLog.info("【机构条件过滤】过滤完成【result】{}【uid】{}【infoId】{}", JSON.toJSONString(handlerResult), memberExtInfo.getUid(), itemInfo.getInfoId());

        return handlerResult;
    }

    /**
     * 执行条件过滤
     * @param memberExtInfo
     * @return
     */
    @Override
    public FilterHandlerResult execSpreadFilter(SpreadTaskInfo taskInfo, MemberExtInfo memberExtInfo){

        FilterHandlerParam handlerParam = new FilterHandlerParam();

        handlerParam.setItemId(String.valueOf(taskInfo.getId()));
        handlerParam.setParentId(String.valueOf(taskInfo.getInfoId()));

        handlerParam.setOriginType("SPREAD");
        handlerParam.setStatus(taskInfo.getStatus());

        handlerParam.setInCityArray(taskInfo.getInCityArray());
        handlerParam.setOutCityArray(taskInfo.getOutCityArray());

        handlerParam.setStartTime(taskInfo.getStarttime());
        handlerParam.setEndTime(taskInfo.getEndtime());

        handlerParam.setMinAge(taskInfo.getMinAge());
        handlerParam.setMaxAge(taskInfo.getMaxAge());

        handlerParam.setZhiMa(taskInfo.getZhima());
        handlerParam.setHuaBei(taskInfo.getHuabei());
        handlerParam.setBaiTiao(taskInfo.getBaitiao());

        handlerParam.setHouse(taskInfo.getHouse());
        handlerParam.setCar(taskInfo.getCar());
        handlerParam.setGjj(taskInfo.getGjj());
        handlerParam.setBaoDan(taskInfo.getBaodan());
        handlerParam.setSheBao(taskInfo.getShebao());

        FilterHandlerResult handlerResult = FilterFactory.execFilterChainService(handlerParam, memberExtInfo);

        return handlerResult;
    }

}
